package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCard;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCardDaoImpl extends TempDataObjectDao<UserCard> {

    public UserCard getUserCard(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_card where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCard data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, UserCard> getUserCardMapByTime(long minTime, long maxTime) {
        Map<Long, UserCard> userCardMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_card where weekCardEndTime between ? and ? or lifeCardEndTime between ? and ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, minTime);
            call.setLong(i++, maxTime);
            call.setLong(i++, minTime);
            call.setLong(i++, maxTime);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCard data = parse(rs);
                long userId = data.getUserId();
                userCardMap.put(userId, data);
            }
            return userCardMap;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    @Override
    protected boolean addData(UserCard data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_card(userId,weekCardEndTime,getWeekCardRewardTime,lifeCardEndTime,getLifeCardRewardTime,realWeekCardEndTime," +
                "weekCardExperiment) values (?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getMonthCardEndTime());
            call.setLong(i++, data.getGetMonthCardRewardTime());
            call.setLong(i++, data.getYearCardEndTime());
            call.setLong(i++, data.getGetYearCardRewardTime());
            call.setLong(i++, data.getWeekCardEndTime());
            call.setBoolean(i++, data.isWeekCardExperiment());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserCard data) {
        PreparedStatement call = null;
        String sql = "update t_u_card set weekCardEndTime=?,getWeekCardRewardTime=?,lifeCardEndTime=?,getLifeCardRewardTime=?," +
                "realWeekCardEndTime=?,weekCardExperiment=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getMonthCardEndTime());
            call.setLong(i++, data.getGetMonthCardRewardTime());
            call.setLong(i++, data.getYearCardEndTime());
            call.setLong(i++, data.getGetYearCardRewardTime());
            call.setLong(i++, data.getWeekCardEndTime());
            call.setBoolean(i++, data.isWeekCardExperiment());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserCard parseData(ResultSet rs) throws SQLException {
        UserCard data = new UserCard();
        data.setUserId(rs.getLong("userId"));
        data.setMonthCardEndTime(rs.getLong("weekCardEndTime"));
        data.setGetMonthCardRewardTime(rs.getLong("getWeekCardRewardTime"));
        data.setYearCardEndTime(rs.getLong("lifeCardEndTime"));
        data.setGetYearCardRewardTime(rs.getLong("getLifeCardRewardTime"));
        data.setWeekCardEndTime(rs.getLong("realWeekCardEndTime"));
        data.setWeekCardExperiment(rs.getBoolean("weekCardExperiment"));
        return data;
    }
}
